package cn.glutnn.controller;

import cn.glutnn.entity.Orders;
import cn.glutnn.entity.Result;
import cn.glutnn.service.OrdersService;
import cn.glutnn.service.PaymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private OrdersService ordersService;

    @Autowired
    private PaymentService paymentService;

    /**
     * 更新订单状态 - 用于直接支付
     */
    @PostMapping("/updateStatus")
    public Result<String> updatePaymentStatus(
            @RequestParam String orderId, 
            @RequestParam String status,
            @RequestParam(required = false) Integer paymentMethodId,
            @RequestParam(required = false) Integer daId) {
        try {
            paymentService.updateOrderStatus(orderId, status, paymentMethodId, daId);
            return Result.success("订单状态更新成功");
        } catch (Exception e) {
            return Result.error("订单状态更新失败: " + e.getMessage());
        }
    }

    /**
     * 查询支付状态
     */
    @GetMapping("/query")
    public Result<String> queryPaymentStatus(@RequestParam String orderId) {
        Orders order = ordersService.getById(orderId);
        if (order == null) {
            return Result.error("订单不存在");
        }

        // 根据订单状态返回支付结果
        if (order.getOrderState() == 1) { // 1表示已支付
            return Result.success("PAID");
        } else {
            return Result.success("UNPAID");
        }
    }
}